﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Lion.WeiXin.Core.Data
{
   public class UserInfoDAO:IUserInfo
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
       public bool Exists(string userName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_UserInfo");
            strSql.Append(" where UserName=@UserName");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)
			};
            parameters[0].Value = userName;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Models.UserInfoModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_UserInfo(");
            strSql.Append("UserName,Password,Phone,Image,RealName,NickName,WeChat_UserName,Gender,Creator)");
            strSql.Append(" values (");
            strSql.Append("@UserName,@Password,@Phone,@Image,@RealName,@NickName,@WeChat_UserName,@Gender,@Creator)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@Password", SqlDbType.VarChar,50),
					new SqlParameter("@Phone", SqlDbType.Char,11),
					new SqlParameter("@Image", SqlDbType.VarChar,255),
					new SqlParameter("@RealName", SqlDbType.VarChar,50),
					new SqlParameter("@NickName", SqlDbType.VarChar,50),
					new SqlParameter("@WeChat_UserName", SqlDbType.VarChar,50),
					new SqlParameter("@Gender", SqlDbType.Char,2),
					new SqlParameter("@Creator", SqlDbType.VarChar,50)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.Password;
            parameters[2].Value = model.Phone;
            parameters[3].Value = model.Image;
            parameters[4].Value = model.RealName;
            parameters[5].Value = model.NickName;
            parameters[6].Value = model.WeChat_UserName;
            parameters[7].Value = model.Gender;
            parameters[8].Value = model.Creator;

            object obj = SqlHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Models.UserInfoModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_UserInfo set ");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Password=@Password,");
            strSql.Append("Phone=@Phone,");
            strSql.Append("Image=@Image,");
            strSql.Append("RealName=@RealName,");
            strSql.Append("NickName=@NickName,");
            strSql.Append("WeChat_UserName=@WeChat_UserName,");
            strSql.Append("Gender=@Gender,");
            strSql.Append("Creator=@Creator,");
            strSql.Append("CreateDate=@CreateDate");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@Password", SqlDbType.VarChar,50),
					new SqlParameter("@Phone", SqlDbType.Char,11),
					new SqlParameter("@Image", SqlDbType.VarChar,255),
					new SqlParameter("@RealName", SqlDbType.VarChar,50),
					new SqlParameter("@NickName", SqlDbType.VarChar,50),
					new SqlParameter("@WeChat_UserName", SqlDbType.VarChar,50),
					new SqlParameter("@Gender", SqlDbType.Char,2),
					new SqlParameter("@Creator", SqlDbType.VarChar,50),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@AID", SqlDbType.Int,4)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.Password;
            parameters[2].Value = model.Phone;
            parameters[3].Value = model.Image;
            parameters[4].Value = model.RealName;
            parameters[5].Value = model.NickName;
            parameters[6].Value = model.WeChat_UserName;
            parameters[7].Value = model.Gender;
            parameters[8].Value = model.Creator;
            parameters[9].Value = model.CreateDate;
            parameters[10].Value = model.AID;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_UserInfo ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string AIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_UserInfo ");
            strSql.Append(" where AID in (" + AIDlist + ")  ");
            int rows = SqlHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.UserInfoModel GetModel(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AID,UserName,Password,Phone,Image,RealName,NickName,WeChat_UserName,Gender,Creator,CreateDate from T_UserInfo ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            Models.UserInfoModel model = new Models.UserInfoModel();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ConvertToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.UserInfoModel ConvertToModel(DataRow row)
        {
            Models.UserInfoModel model = new Models.UserInfoModel();
            if (row != null)
            {
                if (row["AID"] != null && row["AID"].ToString() != "")
                {
                    model.AID = int.Parse(row["AID"].ToString());
                }
                if (row["UserName"] != null)
                {
                    model.UserName = row["UserName"].ToString();
                }
                if (row["Password"] != null)
                {
                    model.Password = row["Password"].ToString();
                }
                if (row["Phone"] != null)
                {
                    model.Phone = row["Phone"].ToString();
                }
                if (row["Image"] != null)
                {
                    model.Image = row["Image"].ToString();
                }
                if (row["RealName"] != null)
                {
                    model.RealName = row["RealName"].ToString();
                }
                if (row["NickName"] != null)
                {
                    model.NickName = row["NickName"].ToString();
                }
                if (row["WeChat_UserName"] != null)
                {
                    model.WeChat_UserName = row["WeChat_UserName"].ToString();
                }
                if (row["Gender"] != null)
                {
                    model.Gender = row["Gender"].ToString();
                }
                if (row["Creator"] != null)
                {
                    model.Creator = row["Creator"].ToString();
                }
                if (row["CreateDate"] != null && row["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                }
            }
            return model;
        }

        protected List<Models.UserInfoModel> ConvertToList(DataSet ds)
        {
            List<Models.UserInfoModel> list = new List<Models.UserInfoModel>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Models.UserInfoModel model = ConvertToModel(row);
                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Models.UserInfoModel> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AID,UserName,Password,Phone,Image,RealName,NickName,WeChat_UserName,Gender,Creator,CreateDate ");
            strSql.Append(" FROM T_UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Models.UserInfoModel> GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" AID,UserName,Password,Phone,Image,RealName,NickName,WeChat_UserName,Gender,Creator,CreateDate ");
            strSql.Append(" FROM T_UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public List<Models.UserInfoModel> GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.AID desc");
            }
            strSql.Append(")AS Row, T.*  from T_UserInfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }
    }
}
